<?php
class id5_note
{
	function id5_note()
	{
		global $memcacheConfig;
		$this->profix="";
		$this->code_profix="code_";
		$this->cache_time = 60*60*24;//保留一天
		$this->mc = Factory::getMemcached();
		$this->profixcheck = "check_";
	}
	/**
		百合下发方式
	*/
	
	function id5_send($icpcode,$mobile,$send_msg,$rs_type=0)
	{
		//获取来源的数据
		$icpcode_arr = get_icpcode_info($icpcode);
		if(empty($icpcode_arr)) return false;

		//设置唯一值
		$uu_id = $this->get_id($icpcode,$mobile);
		
		//获取mc数据
		$mc_data = $this->get_mc_data($uu_id);

		//检测是否超时间隔时间
		$temp = $this->check_send_time($mc_data,$icpcode_arr['interval_time']);
		if(empty($temp))
		{
			$msg = $this->get_time_error($icpcode_arr);
			if($rs_type!=0){
				echo $msg;
				return false;
			}else{
				alertMsg2($msg,'index.php');
			}
			exit();
		}
/*		print_r($icpcode_arr);
		print_r($mc_data);
		exit;*/
		//检测是否超过发送次数
		$temp = $this->check_send_count($mc_data,$icpcode_arr['send_count']);
		if(empty($temp))
		{
			
			$msg = $this->get_count_error($icpcode_arr);
			if($rs_type!=0){
				echo $msg;
			}else{
				alertMsg2($msg,'index.php');
			}
			exit();
		}
		//检测是否满足发送间隔
		if(empty($mc_data))
		{
			$mc_data = $this->init_info($send_msg,$mobile);
		}
		//调用下发程序
		$result = call_user_func(array($this,$icpcode_arr['send_sms_type']),$mobile,$send_msg,$icpcode_arr['send_sms_url']);
		//拿到结果调用解析函数
		$result = call_user_func(array($this,$icpcode_arr['send_sms_parse']),$result);
/*		echo "1111";
		print_r($result);
		exit;*/
		if($result)
		{
			//更新mc
			$this->update_mc($uu_id,$mc_data,$send_msg);
			//验证码
			$this->mc->set($this->code_profix.$send_msg,$mobile,$icpcode_arr['code_over_time']);
		}
		return $result;
	}
	//下发验证码检测
	function check_code($code,$mobile)
	{
		$temp = $this->mc->get($this->code_profix.$code);
		return $temp==$mobile;
	}
	function update_mc($mc_id,$mc_data,$send_msg)
	{
		$mc_data['send_time'] = time();
		$mc_data['send_count'] = $mc_data['send_count']+1;
		$mc_data['msg'] = $send_msg;
		$this->set_mc_data($this->profixcheck.$mc_id,$mc_data);
	}
	//百合下发程序
	function _get_baihe($mobile,$send_msg,$send_url)
	{
		$send_msg = "msg=<msgcontent>尊敬的百合网用户，欢迎您进行身份通实名认证，您的短信验证码是{$send_msg}</msgcontent><desttermid>{$mobile}</desttermid>";
		$send_msg = iconv('utf-8','gbk',$send_msg);
		$send_url = $send_url.$send_msg;
		//return $send_url;
		return file_get_contents($send_url);
	}
	function _parse_baihe($send_result)
	{
		return trim($send_result)=='<success>0</success>';
	}
	//获得唯一值
	function get_id($icpcode,$mobile)
	{
		return "{$this->profix}_{$icpcode}_{$mobile}";
	}
	//检测下发次数
	function check_send_count($mc_data,$send_count)
	{
		if(empty($mc_data)) return true;
		$cur_send_count = intval($mc_data['send_count']);
		return intval($send_count)>$cur_send_count;
	}
	//下发间隔检测
	function check_send_time($mc_data,$interval_time)
	{
		//return false;
		if(empty($mc_data)) return true;
		$cur_time = time();
		$send_time = $mc_data['send_time'];
		return ($cur_time-$send_time) > $interval_time;
	}
	//初始化存储数据格式
	function init_info($send_msg,$mobile)
	{
		$temp = array();
		//手机号
		$temp["mobile"]= $mobile;
		//下发信息
		$temp['msg'] = $send_msg;
		//建立时间
		$temp['create_time'] = time();
		//下发时间
		$temp['send_time'] = time();
		//下发次数
		$temp['send_count'] = 0;
		return $temp;
	}
	//获取mcdata
	function get_mc_data($mc_id)
	{
		//return array();
		$temp = $this->mc->get($this->profixcheck.$mc_id);
		return @unserialize($temp);
	}
	//设置mcdata
	function set_mc_data($mc_id,$data)
	{
		//return true;
		$temp = serialize($data);
		return $this->mc->set($mc_id,$temp,$this->cache_time);
	}
	/**
	 * 格式化错误信息
	 *
	 * @param unknown_type $icpcode_arr
	 * @return unknown
	 */
	function get_time_error($icpcode_arr)
	{
		extract($icpcode_arr);
		return str_replace("{interval_time}",$interval_time,$icpcode_arr['interval_bug_msg']);
	}
	
	function get_count_error($icpcode_arr)
	{
		extract($icpcode_arr);
		return $icpcode_arr['send_count_bug_msg'];
	}
}
/*
function get_icpcode_info($icpcode){
	$coop_arr['20111201'] = array('name'=>'百合网',//合作伙伴名称
							  'call_back_url' =>'http://auth.baihe.com/id5/authedId5.action',//合作伙伴请求地址
							  'key' =>'7b62ac4b408f7',//合作伙伴预留码
							  'icpcode'=>'20111201', 
							  'logo'=>'baohe_logo.gif',  //展示 logo
							  'info'=>'百合网',   // 展示 title
							  'send_sms_type'=>'_get_baihe', //下发类型 ,
							  'send_sms_parse'=>'_parse_baihe',//返回结果解析
							  'send_sms_url' => 'http://sms.baihe.com/smssend/sendmsg.jsp?', //下发地址
							  'interval_time'=>60,//下发间隔
							  'send_count'=>10,//重发次数
							  'interval_bug_msg'=>'对不起,{interval_time}秒以后才可以重发',
							  'send_count_bug_msg'=>'对不起，您已超过今天发送短信次数',
						);
	return $coop_arr[$icpcode];
}
$sms = new id5_note();

$sms->id5_send('20111201','13911167861',"sanshi");
*/
?>